<template>
  <div>
    <!-- 信用总览 -->
    <el-card shadow="never" style="margin-bottom: 20px;">
      <div slot="header">
        <span>我的信用</span>
      </div>
      <el-row :gutter="20" type="flex" align="middle">
        <el-col :span="10">
          <el-progress type="dashboard" :percentage="user.creditScore" :width="150"></el-progress>
        </el-col>
        <el-col :span="14">
          <el-descriptions :column="1" border>
            <el-descriptions-item label="当前信用分">{{ user.creditScore }}</el-descriptions-item>
            <el-descriptions-item label="当前等级">{{ creditInfo.level }}</el-descriptions-item>
            <el-descriptions-item label="等级权益">
              <el-tag size="small">{{ creditInfo.benefit }}</el-tag>
            </el-descriptions-item>
          </el-descriptions>
        </el-col>
      </el-row>
    </el-card>

    <!-- 信用规则与历史 -->
    <el-card shadow="never">
       <el-tabs>
        <el-tab-pane label="信用历史">
          <el-table :data="creditHistory" style="width: 100%">
            <el-table-column prop="cause" label="变化原因" width="180"></el-table-column>
            <el-table-column prop="deductPoints" label="扣除分数" width="180"></el-table-column>
            <el-table-column prop="finalScore" label="扣除后分数" width="180"></el-table-column>
            <el-table-column prop="createTime" label="扣分时间"></el-table-column>
          </el-table>
        </el-tab-pane>
        <el-tab-pane label="信用规则">
          <h4>信用分如何计算？</h4>
          <ul>
            <li v-for="rule in creditRules" :key="rule.ruleId">
              {{ rule.ruleName }}: {{ rule.basis }}
            </li>
          </ul>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import { getInfo } from "@/api/system/creditLevel";
import { listRule } from "@/api/system/rule";

export default {
  name: "CreditLevel",
  props: {
    user: Object	
  },
  data() {
    return {
      creditInfo: {
        level: '',
        benefit: ''
      },
      creditHistory: [],
      creditRules: []
    };
  },
  created() {
    this.getInfo();
    this.getCreditRules();
  }, 
  methods: {
    getInfo(){
      getInfo().then(response => {
        if (response.data) {
          this.creditInfo.level = response.data.level;
          this.creditInfo.benefit = response.data.benefit;
          this.creditHistory = response.data.gradScoreRecords;
        }
      })
    },
    getCreditRules() {
      listRule({ ruleType: 'credit' }).then(response => {
        this.creditRules = response.rows;
      });
    }
  }
};
</script> 